//****************针对第一种方式的具体js实现部分******************//
//****************所使用的数据是city.js******************//

var prov = document.getElementById('province');
var city = document.getElementById('city');
var country = document.getElementById('country');
var addrShow = document.getElementById('homeAddr');

var city2 = document.getElementById('city2');
var country2 = document.getElementById('country2');
var zrdw = document.getElementById('zrdw');

/*用于保存当前所选的省市区*/
var current = {
    prov: '',
    city: '',
    country: ''
};

/*自动加载省份列表*/
(function showProv() {
    var len = provice.length;
    for (var i = 0; i < len; i++) {
        var provOpt = document.createElement('option');
        provOpt.innerText = provice[i]['name'];
        provOpt.value = i;
        prov.appendChild(provOpt);
    }
    
    //加载广西的信息
    getGuangxiCity();
})();

/*根据所选的省份来显示城市列表*/
function showCity(obj) {
    var val = obj.options[obj.selectedIndex].value;
    if (val != current.prov) {
        current.prov = val;
        addrShow.value = '';
    }
    //console.log(val);
    if (val != null) {
        city.length = 1;
        var cityLen = provice[val]["city"].length;
        for (var j = 0; j < cityLen; j++) {
            var cityOpt = document.createElement('option');
            cityOpt.innerText = provice[val]["city"][j].name;
            cityOpt.value = j;
            city.appendChild(cityOpt);
        }
    }
}

/*根据所选的城市来显示县区列表*/
function showCountry(obj) {
    var val = obj.options[obj.selectedIndex].value;
    current.city = val;
    if (val != null) {
        country.length = 1; //清空之前的内容只留第一个默认选项
        var countryLen = provice[current.prov]["city"][val].districtAndCounty.length;
        if(countryLen == 0){
            addrShow.value = provice[current.prov].name + provice[current.prov]["city"][current.city].name;
            return;
        }
        for (var n = 0; n < countryLen; n++) {
            var countryOpt = document.createElement('option');
            countryOpt.innerText = provice[current.prov]["city"][val].districtAndCounty[n];
            countryOpt.value = n;
            country.appendChild(countryOpt);
        }
    }
}

/*选择县区之后的处理函数*/
function selecCountry(obj) {
    current.country = obj.options[obj.selectedIndex].value;
    showAddr();
}

/*点击确定按钮显示用户所选的地址*/
function showAddr() {
    addrShow.value = provice[current.prov].name + provice[current.prov]["city"][current.city].name + provice[current.prov]["city"][current.city].districtAndCounty[current.country];
}

function getGuangxiCity(){
    var val=20, cityLen = provice[val]["city"].length;
    for (var j = 0; j < cityLen; j++) {
        var cityOpt = document.createElement('option');
        cityOpt.innerText = provice[val]["city"][j].name;
        cityOpt.value = j;
        city2.appendChild(cityOpt);
    }
}

function getGuangxiCountry(obj){
    var val = obj.options[obj.selectedIndex].value, currProv=20;
    zrdw.value='';
    if (val != null) {
        var countryLen = provice[currProv]["city"][val].districtAndCounty.length;

        zrdw.value = provice[currProv]["city"][val].name;

        for (var n = 0; n < countryLen; n++) {
            var countryOpt = document.createElement('option');
            countryOpt.innerText = provice[currProv]["city"][val].districtAndCounty[n];
            countryOpt.value = n;
            country2.appendChild(countryOpt);
        }
    }
}

function chooseCountry(obj) {
    zrdw.value = zrdw.value+obj.options[obj.selectedIndex].innerText;
    
}